//
//  YZMLoginResultModel.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/21.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMLoginResultModel.h"
#import "MBProgressHUD+Utils.h"
#import "Mantle+Utils.h"

#define sYZMCurrentLoginResultDefault  @"YZMCurrentLoginResultDefault"

@implementation YZMLoginResultModel


+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{
             @"authenticated": @"authenticated",
             @"username": @"username",
             @"userID": @"userId",
             @"resultMessage": @"resultMsg",
             };
}

+ (NSValueTransformer *)userIDJSONTransformer
{
    return [NSValueTransformer valueTransformerForName: MTLLongStringTransformerName];
}

- (id)init{
    
    self = [super init];
    if (self) {
        
        self.entranceURL = @"aa";
        self.resultMessage = @"bb";
    }
    
    return self;
}

- (void)showMessage
{
    [MBProgressHUD showMessage: self.resultMessage];
}

- (void)encodeWithCoder:(NSCoder *)coder{
    
    [coder encodeObject:self.userID forKey:@"userID"];
    [coder encodeObject:self.username forKey:@"username"];
}

- (id)initWithCoder:(NSCoder *)coder{
    
    self = [super init];
    if (self) {
        
        self.userID = [coder decodeObjectForKey:@"userID"];
        self.username = [coder decodeObjectForKey:@"username"];
    }
    
    return self;
}

- (void)createEncoder{
    
    NSMutableData *data = [NSMutableData data];
    //创建一个归档类
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
    [archiver encodeObject:self forKey:sYZMCurrentLoginResultDefault];
    [archiver finishEncoding];
    //将数据写入文件里
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:sYZMCurrentLoginResultDefault];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (void)createEncoderNil{
    
    [[NSUserDefaults standardUserDefaults] removeObjectForKey: sYZMCurrentLoginResultDefault];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (id)createDecoder{
    
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:sYZMCurrentLoginResultDefault];
    
    if (data && data.length != 0) {
        
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        
        YZMLoginResultModel *instance = [unarchiver decodeObjectForKey:sYZMCurrentLoginResultDefault];
        [unarchiver finishDecoding];
        
        return instance;
    }
    
    return nil;
}


@end
